package com.itouch.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.itouch.bracelet.R;

public class CCLoadingDialog extends Dialog {

	// logingdialog_imageview
	// logingdialog_textview
	// inner_loadingdialog

	private static CCLoadingDialog customProgressDialog = null;
	private Context context;
	private AnimationDrawable animationDrawable;
	private ImageView imageView;

	public CCLoadingDialog(Context context) {
		super(context);
		this.context = context;
	}

	public CCLoadingDialog(Context context, int theme) {
		super(context, theme);
	}

	public static CCLoadingDialog createDialog(Context context) {
		synchronized (CCLoadingDialog.class) {
			customProgressDialog = new CCLoadingDialog(context,
					R.style.CustomProgressDialog);
			customProgressDialog
					.setContentView(R.layout.library_loadingdialog_layout);
			customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		}
		return customProgressDialog;
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {

		if (customProgressDialog == null) {
			return;
		}

		 imageView = (ImageView) customProgressDialog
				.findViewById(R.id.logingdialog_imageview);
		// RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f,
		// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
		// 0.5f);
		// rotateAnimation.setDuration(1 * 800);
		// rotateAnimation.setRepeatCount(-1);
		// rotateAnimation.setInterpolator(new LinearInterpolator());

		imageView.setImageResource(R.drawable.loading_anim);
		animationDrawable = (AnimationDrawable) imageView.getDrawable();
		animationDrawable.start();

		// imageView.startAnimation(rotateAnimation);
	}

	public void setFinish(){
		if (animationDrawable != null && animationDrawable.isRunning()) {
			animationDrawable.stop();
		}
		if(imageView!=null){
			imageView.setBackgroundResource(R.drawable.ic_launcher);
		}
	}
	
	
	
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
		if (animationDrawable != null && animationDrawable.isRunning()) {
			animationDrawable.stop();
		}

	}

	public CCLoadingDialog setTitile(String strTitle) {
		return customProgressDialog;
	}

	public CCLoadingDialog setMessage(String strMessage) {
		TextView tvMsg = (TextView) customProgressDialog
				.findViewById(R.id.loaingdialog_textview);

		if (tvMsg != null) {
			tvMsg.setText(strMessage);
		}

		return customProgressDialog;
	}

}
